﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using DevExpress.Xpo;
using Zero.Core.Domain.Query;
using Zero.DataLayer.Donnees;
using Zero.POCO.Compagne;
using Zero.POCO.Interfaces;

namespace Zero.DataLayer.Repository
{
    public class PlanTvRepository : IRepositoryPlan<PlanTV, Guid>
    {
        public PlanTV FindBy(Guid id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<PlanTV> FindAll()
        {
            var listEntities = new List<PlanTV>();

            var config = new AppSettingsReader();
            using (
                var uow = new UnitOfWork()
                    {
                        ConnectionString =
                            ((string)config.GetValue("connect", typeof(string)))
                    })
            {
                var xpoEntities = new XPCollection<XpoPlanTv>(uow);
                listEntities.AddRange(MAP.MapInverse.MapPlanTv(xpoEntities));
            }
            return listEntities;
        }

        public IEnumerable<PlanTV> FindBy(Query query)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<PlanTV> FindBy(Query query, int index, int count)
        {
            throw new NotImplementedException();
        }

        public void SaveAll(IEnumerable<PlanTV> entities)
        {
            throw new NotImplementedException();
        }

        public List<PlanTV> FindAll(List<Query> entities)
        {
            throw new NotImplementedException();
        }

        public void Save(PlanTV entity)
        {

            var config = new AppSettingsReader();
            using (
                var uow = new UnitOfWork()
                    {
                        ConnectionString =
                            ((string)config.GetValue("connect", typeof(string)))
                    })
            {
                var comp = MAP.Map.MapplanTv(entity, uow);
                uow.CommitChanges();
            }
        }

        public void Add(PlanTV entity)
        {
            throw new NotImplementedException();
        }

        public void Remove(PlanTV entity)
        {
            throw new NotImplementedException();
        }

        public double GetTarif(Query query)
        {
            double prix = 0;
            var criterias = query.Criteria;
            if (criterias != null)
            {
                var criteriaPlanTv = criterias.First(e => e.PropertyName == "PlanTv");
                var plantv = (PlanTV)criteriaPlanTv.Value;
                var listeMap = plantv.ListeMapSupportTarifs;
                var criteriaInsertionTv = criterias.First(e => e.PropertyName == "InsertionTv");
                var insertiontvs = (List<InsertionTv>)criteriaInsertionTv.Value;

                if (listeMap==null) return 0;
                foreach (var insertionTv in insertiontvs)
                {
                    MapSupportTarif maptarif = null;
                    var tv = insertionTv;
                    IEnumerable<MapSupportTarif> maptarifs = listeMap.Where(e => e.SupportId == tv.SupportTv.id);
                    if (maptarifs.Any())
                        maptarif = maptarifs.FirstOrDefault();
                    else continue;
                    if (maptarif != null)
                    {
                        List<LignesTarifTv> tarifs=null;

                        tarifs=maptarif.TarifTv.LignesTarifTvs.Where(lt => lt.duree == insertionTv.duree).ToList();
                        if(!tarifs.Any())
                            tarifs = maptarif.TarifTv.LignesTarifTvs.Where(lt => lt.duree == 0).ToList(); 
                        if (tarifs.Any())
                        {
                            var totalminutes = insertionTv.NumeroCinQMinute*5;
                            var hours = totalminutes/60;
                            var minutes = totalminutes%60;
                            var debutSpan = new TimeSpan(hours,minutes,0);

                            var LaLigne=tarifs.Where(tr => tr.Debut <= debutSpan && tr.Fin > debutSpan);
                            if (LaLigne.Any())
                                prix = prix + LaLigne.FirstOrDefault().Prix;
                        }

                    }
                }
            }
            return prix;
        }
    }
}